package com.gavin.plugin.service.impl;

import com.gavin.model.Request;
import com.gavin.model.Response;
import com.gavin.model.Result;
import com.gavin.plugin.database.IAtom;
import com.gavin.plugin.service.ITransPlugin;
import com.gavin.plugin.service.ITransService;
import com.gavin.schema.dataservice.DataService;

/**
 * ------------------------------
 * 事务服务
 * ------------------------------
 * @author wdm（l311576@sina.com）  @date 2018年7月26日
 * @version 1.0
 */
public class TransService implements ITransService {
	private String serviceName;
	private DataService dataService;
	private ITransPlugin transPlugin;
	private TransBus transBus;
	
	public TransService(String serviceName, ITransPlugin transPlugin, TransBus transBus) {
		this.serviceName = serviceName;
		this.transPlugin = transPlugin;
		this.transBus = transBus;
	}

	public void setDataService(DataService dataService) {
		this.dataService = dataService;
		this.dataService.setServicePlugin(transPlugin);
	}

	public ITransPlugin getServicePlugin() {
		return transPlugin;
	}

	public TransBus getServiceBus() {
		return transBus;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getServiceName() {
		return this.serviceName;
	}
	
	public Response execute(Request request) {
		try {
			if (this.dataService != null) {
				return  this.dataService.execute(request);
			}
			
//			if (this.mongoService != null) {//处理 mongo
//				Response response=new Response();
//				 this.mongoService.handleTrans(request.model(), response);
//				return response;
//			}
		} finally {
			// if(ret!=null)
			// {
			// this.serviceBus.handlePostfixTrans(modelRequest,modelResponse,ret);
			// }
		}
		return new Response(-1,"请求异常");
	}

	public Response execute(IAtom iAtom) {
		if (this.dataService != null) {
			return  this.dataService.execute(iAtom);
		}
		return new Response(-1,"请求异常");
	}

	public Result start() {
		return Result.OK;
	}

	public void stop() {
	}
}
